//
//  YSHistoryCtrl.m
//  Yueshi
//
//  Created by Northwan on 15/7/16.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  往期首页

#import "YSHistoryCtrl.h"
#import "YSHomeDataTool.h"
#import "YSHistoryCell.h"
#import "YSHistoryHeaderView.h"
#import "YSReviewsCtrl.h"

@interface YSHistoryCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (assign, nonatomic) NSInteger pageIndex;
@property (strong, nonatomic) UITableView *tableView;
@property (strong,nonatomic) NSMutableArray *itemArray;
@property (strong,nonatomic) UIButton *backTopBtn;
@end

@implementation YSHistoryCtrl

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH-NavWithStatusBarH)];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView setBackgroundColor:PUBLIC_BG];
    [self.view addSubview:_tableView];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    header.lastUpdatedTimeLabel.hidden = YES;
    _tableView.header = header;
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    
    _backTopBtn = [[UIButton alloc]init];
    _backTopBtn.frame = CGRectMake(SCREENW-50, SCREENH-200, 40, 40);
    [_backTopBtn setBackgroundImage:[UIImage imageNamed:@"icon_back_top"] forState:UIControlStateNormal];
    [_backTopBtn addTarget:self action:@selector(toTop:) forControlEvents:UIControlEventTouchUpInside];
    [_backTopBtn setHidden:YES];
    [self.view insertSubview:_backTopBtn aboveSubview:self.tableView];
}

-(void)toTop:(UIButton *)toTopBtn{
   [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
                              atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(scrollView.contentOffset.y>scrollView.height){
        [_backTopBtn setHidden:NO];
    }else{
        [_backTopBtn setHidden:YES];
    }
}

-(void)headerRefresh{
    self.pageIndex = 0;
    [self requestData];
}

-(void)footerRefresh{
    self.pageIndex ++;
    [self requestData];
}

-(void)checkRequestData{
    if(!_itemArray.count){
        [self.tableView.header beginRefreshing];
    }
}

-(void)requestData{
    [YSHomeDataTool getHistoryInfo:self.pageIndex success:^(YSHistoryRes *result) {
        [UIUtil refreshComplete:self.tableView];
        if(result.isSucc){
            if(self.pageIndex == 0){
                [self.itemArray removeAllObjects];
                [self.tableView setTableHeaderView:[[YSHistoryHeaderView alloc] initWithResult:result]];
                if(!result.bannerList.count){
                    [self.tableView setY:-YSPublicXInset];
                }
            }
            [self.itemArray addObjectsFromArray:result.reviews];
            [self.tableView reloadData];
        }else{
            ShowEnterTips(result.data);
        }
    } failure:^(NSError *error) {
        [UIUtil refreshComplete:self.tableView];
        ShowTips(kRequestFailure);
    }];
}

#pragma - mark UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSHistoryCell *cell = [YSHistoryCell cellWithTableView:tableView];
    [cell setReviews:self.itemArray[indexPath.row]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     return (SCREENW-YSPublicXInset*2)/1.8 +YSPublicXInset+17+20+YSPublicXInset*2;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YSReviews *reviews =  self.itemArray[indexPath.row];
    YSReviewsCtrl *ctrl = [YSReviewsCtrl initWithReviews:reviews];
    [self.view.superview.viewController.navigationController pushViewController:ctrl animated:YES];
}

@end
